package com.cd.university.config.security.authentication.sms;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @author 陈栋
 * @create 2021/9/11 19:46
 */
public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = 550L;

    private final Object principal;

    /**
     * 封装一个未进行认证的SmsCodeAuthenticationToken
     * @param principal 电话
     */
    public SmsCodeAuthenticationToken(Object principal) {
        super((Collection)null);
        this.principal = principal;
        this.setAuthenticated(false);
    }

    /**
     * 进行认证时调用的SmsCodeAuthenticationToken
     * @param authorities 权限列表
     * @param principal 电话
     */
    public SmsCodeAuthenticationToken(Collection<? extends GrantedAuthority> authorities, Object principal) {
        super(authorities);
        this.principal = principal;
        this.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    /**
     * 覆写getName，原表单登陆时返回的是username
     * @return 返回电话
     */
    @Override
    public String getName() {
        return (String) this.principal;
    }
}
